# 脚本用来生成原始未过滤的 droplets 的 Barcode info 信息 和 counts 与 Feature 的 FeaturePlot 图

if (!require("optparse", quietly = TRUE))
  install.packages("optparse")
library(Seurat)
usage = "Rscript Seurat_get_barcode_info.R --dir dir --samplename samplename --tax tax"
option_list = list(
  make_option("--dir",type = "character",action = "store",default = FALSE,help = "raw or filter outs dir"),
  make_option("--samplename",type = "character",action = "store",default = FALSE,help = "samplename"),
  make_option("--tax",type = "character",action = "store",default = FALSE,help = "human,mouse,rat,pig,cfa,mcc,bta,chx")
)
opt_parser = OptionParser(option_list = option_list,usage = usage)
opt = parse_args(opt_parser)

project_data <- Seurat::Read10X(opt$dir)
project <- Seurat::CreateSeuratObject(counts = project_data,project = opt$samplename,min.cells = 1,min.features = 0)
Bin <- "/public/home/lxw/01.Script/my_script"
genes_list <- read.table("rRNA.gene.list",header=TRUE)
known_tax <- unique(genes_list$Tax)
if( is.null(opt$tax) ){
  project@meta.data |> 
    tibble::rownames_to_column(var = "Symbol") |> 
    dplyr::arrange(desc(nCount_RNA)) |> 
    data.table::fwrite(file = paste0(opt$samplename,"_Barcode_info.xls"),quote = F,sep = "\t",row.names = FALSE, col.names = TRUE)
}else{
  if( opt$tax %in% known_tax ){
    index <- which(genes_list$Tax %in% opt$tax & genes_list$Type %in% "mtRNA")
    mito_genes <- unlist(strsplit(as.character(genes_list[index,3]),split=","))
    index <- which(genes_list$Tax %in% opt$tax & genes_list$Type %in% "rRNA")
    ribo_genes <- unlist(strsplit(as.character(genes_list[index,3]),split=","))
    index <- which(genes_list$Tax %in% opt$tax & genes_list$Type %in% "redcell")
    red_genes <- unlist(strsplit(as.character(genes_list[index,3]),split=","))
    mito_genes <- mito_genes[which(mito_genes %in% rownames(project))]
    ribo_genes <- ribo_genes[which(ribo_genes %in% rownames(project))]
    red_genes <- red_genes[which(red_genes %in% rownames(project))]
    if(length(mito_genes)>=1){
      project[["percent.mt"]] <- PercentageFeatureSet(project, features=mito_genes)
    }else{
      project[["percent.mt"]] <- rep(0,dim(project)[2])
    }
    if(length(ribo_genes)>=1){
      project[["percent.ribo"]] <- PercentageFeatureSet(project, features=ribo_genes)
    }else{
      project[["percent.ribo"]] <- rep(0,dim(project)[2])
    }
    if(length(red_genes)>=1){
      project[["percent.redcell"]] <- PercentageFeatureSet(project, features=red_genes)
    }else{
      project[["percent.redcell"]] <-rep(0,dim(project)[2])
    }
  }
}

p <- Seurat::FeatureScatter(project,feature1 = "nCount_RNA",feature2 = "nFeature_RNA")
ggplot2::ggsave(p,filename = paste0(opt$samplename,"_nCount_nFeature_Featureplot.png"),width = 6,height = 5,dpi = 300)
project@meta.data |> 
  tibble::rownames_to_column(var = "Symbol") |> 
  dplyr::arrange(desc(nCount_RNA)) |> 
  data.table::fwrite(file = paste0(opt$samplename,"_Barcode_info.xls"),quote = F,sep = "\t",row.names = FALSE, col.names = TRUE)